﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

using MySql.Data.MySqlClient;

using Com.Model.Cms;
using Com.Dal.Cms;
namespace Com.Bll.Cms
{
    /// <summary>
    /// 文章操作类
    /// </summary>
    public class ArticleOperater
    {
        private CmsArticleHelper ArticleHelper = new CmsArticleHelper();
        private CmsArticleTitlesHelper ArticleTitleHelper = new CmsArticleTitlesHelper();
        private CmsArticleAuthorHelper ArticleAuthorHelper = new CmsArticleAuthorHelper();
        private CmsArticleTypeHelper ArticleTypeHelper = new CmsArticleTypeHelper();
        private CmsArticleSourceHelper ArticleSourceHelper = new CmsArticleSourceHelper();
        private CmsArticleContentHelper ArticleContentHelper = new CmsArticleContentHelper();
        private CmsArticleDetaileHelper ArticleDetailHelper = new CmsArticleDetaileHelper();
        #region//Article 操作方法
        public ArticleOperater()
        {

        }
        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="article">文章实体类</param>
        /// <returns></returns>
        public int InsertArticle(Cms_Article article)
        {
            int result = 0;
            if (article != null)
            {
                #region//持久化数据
                try
                {
                    if (article.ArticleId > 0)
                    {
                        //更新
                        result = ArticleHelper.Update(article);
                    }
                    else
                    {
                        //插入
                        article.CreateDate = DateTime.Now;
                        article.ArticleStateId = 1;

                        result = ArticleHelper.Insert(article).ArticleId;
                    }
                }
                catch
                {
                    result = -1;
                }
                #endregion
            }
            else
            {
                result = -1;
            }
            return result;
        }
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <returns></returns>
        public int DeleteArticle(Cms_Article entity)
        {
            int result = 0;
            try
            {
                result = ArticleHelper.Delete(entity);
            }
            catch
            {
                result = -1;
            }
            return result;
        }
        /// <summary>
        /// 获得一个Article实体
        /// </summary>
        /// <param name="articleId">文章id</param>
        /// <returns></returns>
        public Cms_Article GetArticle(int articleId)
        {
            Cms_Article entity = null;
            try
            {
                entity = ArticleHelper.Find(articleId);
            }
            catch
            {
                entity = null;
            }
            return entity;
        }

        /// <summary>
        /// 查询文章列表
        /// </summary>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页显示数据数目</param>
        /// <param name="where">查询条件</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns></returns>
        public List<V_ArticleDetail> GetArticles(int pageIndex, int pageSize, Expression<Func<V_ArticleDetail, bool>> where, Expression<Func<V_ArticleDetail, Int32>> orderBy)
        {
            List<V_ArticleDetail> list = new List<V_ArticleDetail>();
            try
            {
                list = ArticleDetailHelper.FindList(pageIndex, pageSize, where, orderBy).ToList();
            }
            catch
            {
                list = new List<V_ArticleDetail>();
            }
            return list;
        }
        /// <summary>
        /// 返回符合条件的数据数量
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns>符合条件的数据数量</returns>
        public int GetArticleCount(Expression<Func<V_ArticleDetail, bool>> where)
        {
            int articleCount = 0;
            try
            {
                articleCount = ArticleDetailHelper.FindCount(where);
            }
            catch
            {
                articleCount = -1;
            }
            return articleCount;

        }
        #endregion

        #region//ArticleTitle 操作方法
        /// <summary>
        /// 插入一个文章标题
        /// </summary>
        /// <param name="titles">文章标题对象</param>
        /// <returns>文章标题对象id</returns>
        public int InsertArticleTitle(Cms_ArticleTitles titles)
        {
            int articleTitleId = 0;
            try
            {
                articleTitleId = ArticleTitleHelper.Insert(titles).ArticleTitleId;
            }
            catch
            {
                articleTitleId = -1;
            }
            return articleTitleId;
        }
        /// <summary>
        /// 获得一个文章的标题
        /// </summary>
        /// <param name="articleId">文章标题id</param>
        /// <returns>文章标题对象</returns>
        public Cms_ArticleTitles GetArticleTitles(int articleTitleId)
        {
            Cms_ArticleTitles articleTitles = null;
            try
            {
                articleTitles = ArticleTitleHelper.Find(articleTitleId);
            }
            catch
            {
                articleTitles = new Cms_ArticleTitles();
            }
            return articleTitles;
        }

        /// <summary>
        /// 根据文章id得到文章的标题实体 
        /// </summary>
        /// <param name="articleId">文章id</param>
        /// <returns></returns>
        public Cms_ArticleTitles GetArticleTitlesByArticleId(int articleId)
        {
            Cms_ArticleTitles articleTitles = null;
            try
            {
                int articleTitleId = ArticleHelper.Find(articleId).ArticleTitleId;
                articleTitles = ArticleTitleHelper.Find(articleTitleId);
            }
            catch
            {
                articleTitles = new Cms_ArticleTitles();
            }
            return articleTitles;
        }
        /// <summary>
        /// 更新一个文章标题
        /// </summary>
        /// <param name="titles">文章标题对象</param>
        /// <returns>受影响的记录数</returns>
        public int UpdateArticleTitles(Cms_ArticleTitles titles)
        {
            int affect = 0;
            try
            {
                affect = ArticleTitleHelper.Update(titles);
            }
            catch
            {
                affect = 0;
            }
            return affect;
        }
        #endregion

        #region//ArticleAuthor 操作方法
        public int InsertAuthor(Cms_ArticleAuthor author)
        {
            int authorId = 0;
            try
            {
                authorId = ArticleAuthorHelper.Insert(author).AuthorId;
            }
            catch
            {
                authorId = -1;
            }
            return authorId;
        }
        public int GetAuthorId(String authorName)
        {
            try
            {
                Cms_ArticleAuthor author = ArticleAuthorHelper.Find(item => item.AuthorName.Contains(authorName));
                if (author != null)
                {
                    return author.AuthorId;
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
        public Cms_ArticleAuthor GetAuthor(int authorId)
        {
            Cms_ArticleAuthor author = null;
            try
            {
                author = ArticleAuthorHelper.Find(authorId);
            }
            catch
            {
                return null;
            }
            return author;
        }
        #endregion

        #region//ArticleSouce 操作方法
        /// <summary>
        /// 根据 SourceName 得到SourceId
        /// </summary>
        /// <param name="sourceName">来源字符串</param>
        /// <returns></returns>
        public int GetArticleSourceId(String sourceName)
        {
            int sourceId = 0;
            try
            {
                sourceId = ArticleSourceHelper.Find(item => item.SourceName == sourceName).SourceId;
            }
            catch
            {
                sourceId = 0;
            }
            return sourceId;
        }
        /// <summary>
        /// 增加一个文章来源
        /// </summary>
        /// <param name="sourceName">文章来源</param>
        /// <returns>文章来源id</returns>
        public int InsertArticleSource(String sourceName)
        {
            int sourceId = 0;
            try
            {
                Cms_ArticleSource model = new Cms_ArticleSource();
                model.SourceName = sourceName;
                sourceId = ArticleSourceHelper.Insert(model).SourceId;
            }
            catch
            {
                sourceId = 0;
            }
            return sourceId;
        }
        /// <summary>
        /// 根据文章来源id,得到文章来源实体
        /// </summary>
        /// <param name="sourceId">来源id</param>
        /// <returns>文章来源对象</returns>
        public Cms_ArticleSource GetArticleSource(int sourceId)
        {
            Cms_ArticleSource model = new Cms_ArticleSource();
            try
            {
                model = ArticleSourceHelper.Find(sourceId);
                return model;
            }
            catch
            {
                model = new Cms_ArticleSource();
            }
            return model;
        }
        #endregion

        #region//ArticleType 操作方法
        /// <summary>
        /// 获得所有文章类型
        /// </summary>
        /// <returns></returns>
        public List<Cms_ArticleType> GetArticleTypes()
        {
            List<Cms_ArticleType> articleTypes = new List<Cms_ArticleType>();
            try
            {
                articleTypes = this.ArticleTypeHelper.FindList().ToList();
            }
            catch
            {
                articleTypes = new List<Cms_ArticleType>();
            }
            return articleTypes;
        }
        #endregion

        #region//ArticleContent操作方法
        /// <summary>
        /// 插入文章的一页内容
        /// </summary>
        /// <param name="content">文章内容实体</param>
        /// <returns></returns>
        public int InsertArticleContent(Cms_ArticleContent content)
        {
            try
            {
                return ArticleContentHelper.Insert(content).Id;
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// 删除一篇文章的一页内容
        /// </summary>
        /// <param name="articleId"></param>
        /// <param name="articlePageIndex"></param>
        /// <returns></returns>
        public int DeleteArticleContent(int articleId, int articlePageIndex)
        {
            try
            {
                String sql = "Delete From Cms_ArticleContent Where ArticleId=@ArticleId And PageIndex=@PageIndex";
                MySqlParameter[] ps = { 
                                            new MySqlParameter("@ArticleId",MySqlDbType.Int32),
                                            new MySqlParameter("@PageIndex",MySqlDbType.Int32)
                                      };
                ps[0].Value = articleId;
                ps[1].Value = articlePageIndex;
                return ArticleContentHelper.Delete(sql, ps);
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// 更新文章内容
        /// </summary>
        /// <param name="content">文章内容实体</param>
        /// <returns></returns>
        public int UpdateArticleContent(Cms_ArticleContent content)
        {
            try
            {
                return ArticleContentHelper.Update(content);
            }
            catch
            {
                return -1;
            }
        }
        #endregion
    }
}
